package com.filter.control.controller;

import com.filter.control.common.HttpProxyClient;
import com.filter.control.common.Result;
import com.filter.control.po.ContainerQuotaUpdatePO;
import com.filter.control.service.FilterServiceImpl;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 教学拦截器
 * @author
 */
@RestController
@RequestMapping("tes/container")
public class RunFilterController {

    @Autowired
    private HttpProxyClient httpProxyClient;

    @Autowired
    private FilterServiceImpl tesServiceImpl;

    @Value("${proxyStr}")
    private String proxyStr;

    @GetMapping("running")
    public Result<String> queryContainerQuota(HttpServletRequest request) throws ParseException {
        // 执行请求被代理系统
        String url = proxyStr + "/tes/container/running";
        String str = httpProxyClient.sendMessagesNew(request, url);
        // 解析 JSON 字符串
        JSONParser parser = new JSONParser();
        JSONObject jsonObject = (JSONObject) parser.parse(str);
        // 获取 "data" 的值
        String data = (String) jsonObject.get("data");
        String[] split = data.split("/");
        Integer containerQuota = tesServiceImpl.getContainerQuota(FilterServiceImpl.TES);

        return Result.success(split[0] + "/" + containerQuota);
    }
}
